﻿

import Request from "../../base/request.js?t=202511041006"
import Storage from "../../base/storage.js?t=202511041006"
import { downloadfun, IgetDeptList, queryURLParams, dealTime, getBrowserInfo} from "../../base/utils.js?t=202511041006"
const Composition = (proxy) => {
    const data = reactive({

        messageTest: "",
        proxy: "",
        popShow: false,
        settingList: [
        ],
        uploadImg: {
            previewVisible: false,
            previewImage: "",
            previewTitle: "",
            fileList: [

            ]
        },
        levelList:[],
        uploadLock: false,
        formData: {
            SupplyName: null,
            LevelId: null,
            DepartmentId: null
        },
        deptList: [],

        urlinfo: {},
        jobList:[]

    })
    let params = toRefs(data)
    const methods = {
        getDeptList: () => {
            return new Promise(async (resolve, rej) => {
                params.deptList.value = await IgetDeptList(Request)

            })
        },
        typeChange: () => {
            methods.IGetEmergencyTeamTitle()
            params.settingList.value=[]
        },
        ITeamType: () => {
            return new Promise((resolve, rej) => {
                params.uploadLock.value = true
                Request({
                    url: "/EmergencyPlan/GetEmergencyTeamType",
                    method: 'post',
                    data: {
                        type: params.urlinfo.value.type != 9 ? "Add" :"view"
                    }
                }).then(res => {
                    params.levelList.value = res.Data.map(e => ({
                        label: e.ConfigValue,
                        value: e.Id
                    }))
                    methods.IGetEmergencyTeamTitle()
                    resolve()
                }).finally(() => {
                    params.uploadLock.value = false
                })
            })
        },
        addConfig: () => {
            let obj = {
                Name: null,
                Phone: null,
                Title: null,
                JobId: null,
                Remark: null,
                UserNo: null,
            }
            params.settingList.value.push(obj)
        },
        handleDownloadTemplate: () => {
            params.uploadLock.value = true
            downloadfun('应急队伍模板', '../../File/Templete/EmergencyTeam/导入应急队伍.xls').then(() => {
            }).finally(() => {
                params.uploadLock.value = false
            })
        },
        handleBeforeUpload: () => {
            if (!params.formData.value.TypeId) {
                params.messageTest.value.info("请先选择级别")
                return false
            }
        },
        handleImport: (info) => {

            if (!params.uploadLock.value) {
                params.uploadLock.value = true
                let formData = new FormData();
                formData.append('file', info.file.originFileObj)
                formData.append('TypeId', params.formData.value.TypeId)
                formData.append('DepartmentId', params.formData.value.DepartmentId)
                Request({
                    url: `/EmergencyPlan/ImportEmergencyTeam`,
                    method: 'post',
                    data: formData
                }).then(res => {

           
                    params.settingList.value = [
                        ...params.settingList.value,
                        ...res.Data
                    ]

                    params.uploadLock.value = false


                })

            }
        },

        delConfig: (index,item) => {
            if (item.Id) {
                params.uploadLock.value = true
                Request({
                    url: "/EmergencyPlan/DeleteEmergencyTeam",
                    method: 'post',
                    data: {
                        Id: item.Id
                    }
                }).then(() => {
                    params.settingList.value = params.settingList.value.filter((_e, i) => i != index)
                }).finally(() => {
                    params.uploadLock.value = false
                })
            } else {
                params.settingList.value = params.settingList.value.filter((_e, i) => i != index)
            }
          
        },
        filterOption: (val, type) => {
            return `${type.label}`.includes(`${val || ''}`)
        },
        IhandlePdfExport: () => {
            params.uploadLock.value = true

            Request({
                url: "/EmergencyPlan/ExportEmergencyTeamPageList",
                method: 'post',
                data: {
                    FormId: params.urlinfo.value.id
                }
            }).then((res) => {
                params.uploadLock.value = true
                downloadfun('成员信息', res.Data).finally(() => {
                    params.uploadLock.value = false
                })
            }).finally(() => {
                params.uploadLock.value = false
            })


        },
        IGetEmergencyTeamForm: () => {
            return new Promise((resolve, rej) => {
                params.uploadLock.value = true
                Request({
                    url: "/EmergencyPlan/GetEmergencyTeamForm",
                    method: 'post',
                    data: {
                        Id: params.urlinfo.value.id
                    }
                }).then(res => {
                    params.formData.value = res.Data
                    params.formData.value.user = `${res.Data.CreateUserName}(${res.Data.UpdateUser})`
                    params.formData.value.time = dealTime(res.Data.UpdateTime, 'YYYY-MM-DD HH:mm')
                    params.settingList.value = res.Data.TeamMembers.map(e => {
                        e.ValidDate = dayjs(dealTime(e.ValidDate))
                        return e
                    })
                    params.uploadLock.value = false
                    resolve()

                })
            })

        },

        handleAddData: (data) => {
            params.uploadLock.value = true
            if (params.urlinfo.value.type == 1) {
                Request({
                    url: "/EmergencyPlan/EditEmergencyTeamForm",
                    method: 'post',
                    data: data
                }).then(res => {
                    if (res.Code == 100) {
                        params.messageTest.value.info(res.Msg)
                        setTimeout(() => {
                            methods.onBack()
                        }, 1000)

                    }
                }).finally(() => {
                    params.uploadLock.value = false
                })
            } else {
                Request({
                    url: "/EmergencyPlan/AddEmergencyTeamForm",
                    method: 'post',
                    data: data
                }).then(res => {
                    if (res.Code == 100) {
                        params.messageTest.value.info(res.Msg)
                        setTimeout(() => {
                            methods.onBack()
                        }, 1000)

                    }
                }).finally(() => {
                    params.uploadLock.value = false
                })
            }




        },

        onSubmit() {
            let obj = {
                model: {
                    ...params.formData.value,
                    Id: params.urlinfo.value.id
                },
                list: params.settingList.value
            }
            if (params.settingList.value.filter(e => Boolean(e.JobId)).map(e => e.JobId).length == params.settingList.value.length) {

                methods.handleAddData(obj)
            } else {

                params.messageTest.value.info('请检查职务是否填写')
            }






        },

        onBack: () => {
            window.location.href = '/EmergencyManagement/ETList'
        },
        IGetEmergencyTeamTitle: () => {
            Request({
                url: "/EmergencyPlan/GetEmergencyTeamTitle",
                method: 'post',
                data: {
                    TypeId: params.formData.value.TypeId
                }
            }).then(res => {
                if (res.Code == 100) {
                    params.jobList.value = res.Data.map(e => ({
                        label: e.ConfigValue,
                        value: e.Id
                    }))
                }
            }).finally(() => {
                params.uploadLock.value = false
            })
        },
        init: () => {
            getBrowserInfo().catch(err => { console.log(err) })
            params.urlinfo.value = queryURLParams()
            let info = Storage.get('AccountInfo', true)
            Request({
                url: "/Account/createJwtToken",
                method: 'post',
                data: {
                    "userName": info && info.UserNo
                }
            }).then(res => {
                Storage.set('token', res.Data, true)
                methods.getDeptList()
          
                if (params.urlinfo.value.type) {
                    methods.IGetEmergencyTeamForm().then(
                        methods.ITeamType()                    )
                } else {
                    methods.ITeamType()
                    params.formData.value.user = `${info.UserName}(${info.UserNo})`
                    params.formData.value.time = dayjs().format('YYYY-MM-DD HH:mm')
                    params.formData.value.DepartmentId = info.DepartmentId
                }



            })

            if (proxy) {
                params.proxy.value = proxy
            }
            const { message } = antd
            params.messageTest.value = message
            params.messageTest.value.config({
                top: `10vh`,
                duration: 7,
                maxCount: 1,
                rtl: true,
            });

        },

    }
    const template = {
        pdfPopModel: {
            template: `
           <a-modal v-model:open="pdfOpen" width= "80%" title = "PDF预览" v-on:ok="pdfOpen=false" >
<div style='height:700px'>
                <embed id="pdfviewscreen" :src="pdfurl" type="application/pdf" width="100%" style="height:100%;" />

</div>
  
            </a-modal >
`,
            setup() {
                return {
                    ...params,
                    ...methods,
                };
            }
        },


        bottomBtns: {
            template: `<div class="w-full bg-[#eeeeee] h-50  flex  items-center justify-end pr-4">
                    <a-space>

  <a-popconfirm
    title="确定执行此操作?"
    ok-text="确定"
    cancel-text="取消"
    @confirm="onSubmit(0)"
  >
          <a-button  :loading="uploadLock" v-if="urlinfo.type!=9" type="primary">保存</a-button>
  </a-popconfirm>
  <a-popconfirm
    title="确定执行此操作?"
    ok-text="确定"

    cancel-text="取消"
    @confirm="onSubmit(1)"
  >
          <a-button  :loading="uploadLock" v-if="false" type="primary">发布</a-button>
  </a-popconfirm>
                       
              
                        <a-button v-on:click="onBack" :loading="uploadLock">返回</a-button>
                    </a-space>
                </div>`,
            setup() {

                return {
                    ...params,
                    ...methods,
                };
            }

        }
    }
    return {
        ...params,
        ...methods,
        template
    }
}
export default Composition